package com.xyd.service.impl;

import com.xyd.entity.PageBean;
import com.xyd.entity.Product;
import com.xyd.entity.ProductCategory;
import com.xyd.mapper.ProductCategoryMapper;
import com.xyd.mapper.ProductMapper;
import com.xyd.service.ProductSericve;
import com.xyd.util.MybatisUtil;
import com.xyd.util.TotalPageUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.ibatis.session.SqlSession;

public class ProductServiceImpl implements ProductSericve {

  private SqlSession sqlSession;

  @Override
  public Product getProduct(Product product) {
    sqlSession = MybatisUtil.getSqlSession();
    Product product1 = sqlSession.getMapper(ProductMapper.class).getProduct(product);
    MybatisUtil.closeSqlSession(sqlSession);
    return product1;
  }

  @Override
  public List<Product> getProductById(int id) {
    sqlSession = MybatisUtil.getSqlSession();
    List<Product> list = sqlSession.getMapper(ProductMapper.class).getProductById(id);
    MybatisUtil.closeSqlSession(sqlSession);
    return list;
  }

  @Override
  public PageBean<Product> adminShowProduct(Integer currentPage) {
    PageBean<Product> pb = new PageBean<>();

    sqlSession = MybatisUtil.getSqlSession();
    Integer totalCount = sqlSession.getMapper(ProductMapper.class).findCountProduct();
    MybatisUtil.closeSqlSession(sqlSession);
    pb.setTotalCount(totalCount);

    Integer pageSize = 5;
    pb.setPageSize(pageSize);

    pb.setTotalPage(TotalPageUtil.getTotalPage(totalCount, pageSize));
    pb.setCurrentPage(currentPage);

    sqlSession = MybatisUtil.getSqlSession();
    List<Product> products = sqlSession.getMapper(ProductMapper.class)
        .adminShowProduct((currentPage - 1) * pageSize, pageSize);
    if (products.size() == 0 && totalCount > 0) {
      products = sqlSession.getMapper(ProductMapper.class)
          .adminShowProduct((currentPage - 2) * pageSize, pageSize);
    }
    MybatisUtil.closeSqlSession(sqlSession);
    pb.setList(products);
    return pb;
  }

  @Override
  public boolean adminDeleteProductByEpId(long epId) {
    sqlSession = MybatisUtil.getSqlSession();
    boolean flag = sqlSession.getMapper(ProductMapper.class).adminDeleteProductByEpId(epId);
    sqlSession.commit();
    MybatisUtil.closeSqlSession(sqlSession);
    return flag;
  }

  @Override
  public Product adminGetProductByEpId(Long epId) {
    sqlSession = MybatisUtil.getSqlSession();
    Product product = sqlSession.getMapper(ProductMapper.class).adminGetProductByEpId(epId);
    MybatisUtil.closeSqlSession(sqlSession);
    return product;
  }

  @Override
  public Boolean adminChangeProduct(Product product) {
    sqlSession = MybatisUtil.getSqlSession();
    Boolean flag = sqlSession.getMapper(ProductMapper.class).adminChangeProduct(product);
    if (flag) {
      sqlSession.commit();
    }
    MybatisUtil.closeSqlSession(sqlSession);
    return flag;
  }

  @Override
  public List<Product> getAllProduct() {
    sqlSession = MybatisUtil.getSqlSession();
    List<Product> list = sqlSession.getMapper(ProductMapper.class).getAllProduct();
    MybatisUtil.closeSqlSession(sqlSession);
    return list;
  }

  @Override
  public Boolean adminAddProduct(Product product) {
    sqlSession = MybatisUtil.getSqlSession();
    ProductCategory productCategory = sqlSession.getMapper(ProductCategoryMapper.class)
        .adminGetCategoryByEpcId(product.getEpcId());
    MybatisUtil.closeSqlSession(sqlSession);

    if (!productCategory.getEpcId().equals(productCategory.getEpcParentId())) {
      product.setEpcId(productCategory.getEpcParentId());
      product.setEpcChildId(productCategory.getEpcId());
    } else {
      product.setEpcChildId(productCategory.getEpcId());
    }

    sqlSession = MybatisUtil.getSqlSession();
    Boolean flag = sqlSession.getMapper(ProductMapper.class).adminAddProduct(product);
    if (flag) {
      sqlSession.commit();
    }
    MybatisUtil.closeSqlSession(sqlSession);
    return flag;
  }

  @Override
  public List<Product> getOldProduct(String name) {
    List<Product> itemlist = new ArrayList<>();
    int count = 5;
    if (name != null && name.length() > 0) {
      String[] arr = name.split(",");
      if (arr.length >= 5) {
        for (int i = arr.length - 1; i > arr.length - count; i--) {
          itemlist.add(getProductByEpid1(Long.parseLong(arr[i])));
        }
      } else {
        for (int i = arr.length - 1; i >= 0; i--) {
          itemlist.add(getProductByEpid1(Long.parseLong(arr[i])));
        }
      }
      return itemlist;
    } else {
      return null;
    }
  }

  @Override
  public Product getProductByEpid1(Long id) {
    sqlSession = MybatisUtil.getSqlSession();
    Product product = sqlSession.getMapper(ProductMapper.class).getProductByEpid1(id);
    MybatisUtil.closeSqlSession(sqlSession);
    return product;
  }

  @Override
  public List<Product> getProductByEpIds(String cookieValue) {
    // cookieValue = "商品id，商品数量；商品id，商品数量；..."
    String[] split = cookieValue.split("\\*");
    String[] productIds = new String[split.length];
    for (int i = 0; i < split.length; i++) {
      productIds[i] = split[i].split(",")[0];
    }
    sqlSession = MybatisUtil.getSqlSession();
    List<Product> products = sqlSession.getMapper(ProductMapper.class)
        .getProductByEpIds(productIds);
    MybatisUtil.closeSqlSession(sqlSession);
    for (Product product : products) {
      for (int i = 0; i < split.length; i++) {
        String[] split1 = split[i].split(",");
        if (product.getEpId().equals(Long.parseLong(split1[0]))) {
          product.setNum(Integer.parseInt(split1[1]));
          break;
        }
      }
    }
    return products;
  }

}
